package PSQueue;

import java.util.ArrayList;

import Service.Variable;

/**
 * @author cyye
 *
 */
public class PSPublication implements AbstractPublication{

	private ArrayList<AbstractMessage> msglist;
	
	public PSPublication() {
		msglist = new ArrayList<AbstractMessage>();
	}	

	/*
	public ArrayList<AbstractMessage> getMsglist() {
		return msglist;
	}
	*/
	
	public AbstractMessage getMessage(String name){		
		assert name != null;
		for(int i=0;i<msglist.size();i++) {
			if(name.equals(msglist.get(i).getMsgName())) 
				return msglist.get(i);
		}
		return null;
	}
	
	public void addMessage(AbstractMessage msg) {
		assert msg!=null;
		PSMessage t_msg = new PSMessage();
		
		//copy msg
		String name = msg.getMsgName();
		assert name!=null;		
		t_msg.setMsgName(name);
		
		int type = msg.getMsgType();
		assert Variable.validType(type);
		t_msg.setMsgType(type);
		
		Object value = msg.getValue();
		assert value!=null;
		t_msg.setValue(value);
		
		if(getMessage(name)!=null) {
		   System.out.println("Invalid Message: Message " + name + " already exists!");	
		} else
		   msglist.add(t_msg);
	}

	@Override
	public ArrayList<String> getMsgIDList() {
		// TODO Auto-generated method stub
		ArrayList<String> ids = new ArrayList<String>();
		for(int i=0;i<msglist.size();i++) {
			ids.add(msglist.get(i).getMsgName());
		}
		return ids;
	}
}
